package org.southyin.service.impl;

import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;
import org.southyin.dao.ClassesDao;
import org.southyin.dao.ClassesDaoImpl;
import org.southyin.model.Classes;
import org.southyin.service.ClassesService;
import org.southyin.utils.DBUtils;
import java.sql.SQLException;
import java.util.List;

public class ClassServiceImpl implements ClassesService {
    ClassesDao dao = new ClassesDaoImpl();
    @Override
    public int addClasses(Classes classes) {
        int i = 0;
        try {
            // 获取连接
            DBUtils.conn = DBUtils.getInstance().getConnection();
            // 控制事务
            DBUtils.conn.setAutoCommit(false);
            // 调用 dao 完成新增
            i = dao.addClasses(classes);
            // 提交事务
            DBUtils.conn.commit();
        } catch (SQLException e) {
            // 回滚事务
            if (DBUtils.conn != null) {
                try {
                    DBUtils.conn.rollback();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            DBUtils.getInstance().close(DBUtils.conn);
        }
        return i;
    }

    @Override
    public Classes getClassInfoById(Long id) {
        Classes classes = null;
        try {
            // 获取连接
            DBUtils.conn = DBUtils.getInstance().getConnection();
            classes = dao.getClassInfoById(id);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            DBUtils.getInstance().close(DBUtils.conn);
        }
        return classes;
    }

    @Override
    public List<Classes> getClasses(String cname) {
        List<Classes> list = null;
        try {
            // 获取连接
            DBUtils.conn = DBUtils.getInstance().getConnection();
            list = dao.getClasses(cname);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            DBUtils.getInstance().close(DBUtils.conn);
        }
        return list;
    }

    @Override
    public int deleteClassById(Long id) {
        int i = 0;
        try {
            // 获取连接
            DBUtils.conn = DBUtils.getInstance().getConnection();
            // 控制事务
            DBUtils.conn.setAutoCommit(false);
            // 调用 dao 完成新增
            i = dao.deleteClassById(id);
            // 提交事务
            DBUtils.conn.commit();
        } catch (SQLException e) {
            if (e instanceof MySQLIntegrityConstraintViolationException) {
                System.out.println("有关联数据");
            }
            // 回滚事务
            if (DBUtils.conn != null) {
                try {
                    DBUtils.conn.rollback();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            DBUtils.getInstance().close(DBUtils.conn);
        }
        return i;
    }
}
